package com.sctvcloud.dongqu.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.flyco.tablayout.SlidingTabLayout;
import com.google.gson.Gson;
import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.application.Cache;
import com.sctvcloud.dongqu.base.BaseFragment;
import com.sctvcloud.dongqu.beans.FocusColumn;
import com.sctvcloud.dongqu.beans.HomeEvent;
import com.sctvcloud.dongqu.ui.activities.ColumnEditActivity;
import com.sctvcloud.dongqu.ui.activities.LoginActivity;
import com.sctvcloud.dongqu.ui.adapter.LoveFlowerPagerAdapter;
import com.sctvcloud.dongqu.ui.adapter.PlateFormPagerAdapter;
import com.sctvcloud.dongqu.ui.dialog.ChooseCityFuncFragment;
import com.sctvcloud.dongqu.utils.UserManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 爱花城
 */
public class MoreCountryFragment extends BaseFragment {

    @BindView(R.id.flower_tablay)
    protected SlidingTabLayout tablay;
    @BindView(R.id.content_vp)
    protected ViewPager content_vp;
    @BindView(R.id.top_lay_tab)
    protected ViewGroup tabLay;
    @BindView(R.id.content_ll)
    protected LinearLayout content_ll;
    
    private LoveFlowerPagerAdapter pagerAdapter;
    private ArrayList<FocusColumn> list = new ArrayList<FocusColumn>();
//    private ArrayList<FocusColumn>  thisColumnList = new ArrayList<FocusColumn>();
    public static final int REQUEST_CODE = 1;
    private Gson gson;


    protected static final int CODE_REQUEST_LOGIN = 303;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_love_flower_city, container, false);
        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //初始化请求栏目数据
        initData();

    }

    @Override
    public void onResume() {
        super.onResume();

//        if(content_vp.getCurrentItem()==2){
//            if (!UserManager.isLoginS()) {
//                content_vp.setCurrentItem(0);
//            }
//        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        ColumnBaseFragment cbf = getCurrentFragment();
        if (!hidden) {
            Cache.getInstance().setCurrentChannelId(Cache.getInstance().getCurrentColumnId("爱花城", "微平台"));
        }else{

        }
        if (cbf != null) {
            cbf.setParentShowing(!hidden);
            cbf.onHiddenChanged(hidden);
        }
    }

    private void initData(){

        FocusColumn flower = new FocusColumn();
        flower.setChannelName("游花城");
        flower.setChannelType(String.valueOf(LoveFlowerPagerAdapter.TYPE_FLOWER_CITY));
        flower.setChannelId(1975);
        flower.setChannelUrl("/sctv/youhuacheng/index/youhuacheng.json");

        FocusColumn food = new FocusColumn();
        food.setChannelName("享美食");
        food.setChannelType(String.valueOf(LoveFlowerPagerAdapter.TYPE_ENJOY_FOOD));
        food.setChannelId(1978);
        food.setChannelUrl("/sctv/xiangmeishi/index/xiangmeishi.json");

        FocusColumn kandong = new FocusColumn();
        kandong.setChannelName("侃东区");
        kandong.setChannelType(String.valueOf(LoveFlowerPagerAdapter.TYPE_KAN_DONG_DISTRICT));
        kandong.setChannelId(1979);
        kandong.setChannelUrl("/sctv/kandongqu/index/kandongqu.json");

        list.add(flower);
        list.add(food);
        list.add(kandong);

        setData(list);
    }

    /**
     * 加载 首页中的各个栏目页面
     *
     * @param fChannels
     */
    private void setData(List<FocusColumn> fChannels) {
        if (ListUtils.isListValued(fChannels)) {
            content_ll.setVisibility(View.VISIBLE);

            boolean b = true;
            int s = -1;
            if (pagerAdapter == null) {
                pagerAdapter = new LoveFlowerPagerAdapter(getChildFragmentManager(), list);
//                setAdapterTopHeiht(true);
                content_vp.setAdapter(pagerAdapter);
                content_vp.setOffscreenPageLimit(1);
                tablay.setViewPager(content_vp);
                s = pagerAdapter.getCount();
            } else {
                s = pagerAdapter.getCount();
//                setAdapterTopHeiht(true);
                pagerAdapter.setData(list);
                b = s != pagerAdapter.getCount();
            }
//            tablay.setOnTabSelectListener(this);
            refreshColumnTab(b);
            tablay.setTextUnselectColor(getResources().getColor(R.color.color_666666));
            tablay.setTextSelectColor(getResources().getColor(R.color.colorBlack22));
            tablay.setChangeSize(true);
        }


        //    tablay.setChangeSize(true);
        tablay.notifyDataSetChanged();
        tablay.setTabPadding(0);
        tablay.setTabSpaceEqual(true);
    }

    /**
     * 获取当前正显示的Fragment
     *
     * @return
     */
    public ColumnBaseFragment getCurrentFragment() {
        if (content_vp == null || pagerAdapter == null || pagerAdapter.getCount() == 0) {
            return null;
        }
        ColumnBaseFragment cbf = pagerAdapter.getFragmentByPos(content_vp.getCurrentItem());
        JLog.e("ffsf " + cbf);

        return cbf;
    }


    /**
     * 刷新栏目在首页的标签
     *
     * @param b
     */
    private void refreshColumnTab(boolean b) {
        int size = pagerAdapter.getCount();
        if (size == 0 || !b) {
            return;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//            topLay.setVisibility(View.GONE);
//            tabLay.setVisibility(View.GONE);
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//            topLay.setVisibility(View.VISIBLE);
//            tabLay.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public boolean hasShowPlayer() {
        BaseFragment bf = getCurrentFragment();
        return bf != null && bf.hasShowPlayer();
    }




    @Override
    public boolean onBackPressed() {
        ColumnBaseFragment f = getCurrentFragment();
        if (f != null && f.onBackPressed()) {
            return true;
        }
        return super.onBackPressed();
    }

    private Gson initGson() {
        if (gson == null) {
            gson = new Gson();
        }
        return gson;
    }
//
//
//    private void setAdapterTopHeiht(boolean isNewInstance) {
//        if (isNewInstance) {
//            pagerAdapter.setTopHeight(tabLay.getHeight());
//        }
//    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onjump(HomeEvent messageEvent) {
        if (null != messageEvent ) {
            if (messageEvent.getPosition()==0) {
                content_vp.setCurrentItem(0);
            }else if(messageEvent.getPosition()==1){
                content_vp.setCurrentItem(1);
            }
        }

    }

}
